//
//  MeatShopTransferViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/30.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatShopTransferViewController.h"

#import "MeatPayViewController.h"

#import "MeatOrderListCell.h"
#import "MeatOrderLocationCell.h"
#import "MeatPayView.h"
#import "MeatTipLabel.h"

#import "MeatOrderBox.h"


@interface MeatShopTransferViewController ()

/**
 tap
 */
@property (nonatomic, strong) UITapGestureRecognizer *tap;


/**
 当前是第几个section
 */
@property (nonatomic, assign) NSInteger currentIndex;


/**
 提示标签
 */
@property (nonatomic, strong) MeatTipLabel *tipLabel;


/**
 footer
 */
@property (nonatomic, strong) MeatPayView *footer;


/**
 icons
 */
@property (nonatomic, strong) NSArray *icons;


/**
 titles
 */
@property (nonatomic, strong) NSArray *titles;


/**
 detailTitles
 */
@property (nonatomic, strong) NSArray *detailTitles;



@end

@implementation MeatShopTransferViewController

#pragma mark - lifecycle methods

- (void)setupUI{
    
    self.currentIndex = 0;
    [self.view addSubview:self.footer];
    
    CGFloat h = KScreenHeight - (TJ_MeatShop_Header + _footer.height + 42);
    self.tableView.height = h;
    self.footer.mj_y = self.tableView.bottom;
}

#pragma mark - networking methods
#pragma mark - response methods
#pragma mark - public methods

#pragma mark - tableview delegate/dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titles.count +1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) return 52;
    if (indexPath.row == self.titles.count) return 44;
    return 32.5;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = mBackgroudColor;
    return sectionView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        BaseIndentationTableCell *newsCell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell11"];
        if (!newsCell) {
            newsCell = [[BaseIndentationTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell11"];
        }
        
        if ([newsCell respondsToSelector:@selector(setSeparatorInset:)]) {
            [newsCell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
        }
        

        newsCell.textLabel.font = mFont(16);
        newsCell.textLabel.textColor = mMainTextColor;
        newsCell.textLabel.text = @"三元仔猪";
        newsCell.textLabel.backgroundColor = KYellowColor;
        newsCell.imageView.image = [UIImage imageNamed:@"meat_shop_transferIcon"];
        
        NSMutableAttributedString *aims =  [@"三元仔猪 " createAttributingByIcon:nil downIndexImage:@"meat_shop_owe_bg" textColor:mMainTextColor font:mFont(13) imageText:@"欠费" imageTextColor:UIColorFromRGB(0xF1925D) imageTextFont:mFont(10)];
        newsCell.textLabel.attributedText = aims;
        
       
        
        newsCell.accessoryView = ({
            UIButton *button = [UIButton makeButtonWithText:@"查看档案" textColor:UIColorFromRGB(0x454545) font:mFont(13) textAliment:1 cornerRadius:15];
            button.layer.borderWidth = 1;
            button.layer.borderColor = UIColorFromRGB(0xD8D8D8).CGColor;
            CGRect frame = button.frame;
            frame.size = CGSizeMake(82, 30);
            button.frame = frame;
            [button setContentMode:UIViewContentModeLeft];
            
            kWeakSelf(self);
            button.tapBlock = ^{
                
                NSLog(@"去充值");
            };
            button;
        });

        return newsCell;
        
    }else{
        
        BaseIndentationTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        if (!cell) {
            
            cell = [[BaseIndentationTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
        }
        
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            
            if (indexPath.row == self.titles.count -1) {
                [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
                 NSMutableAttributedString *aims =  [@"¥1834.98 " createAttributingByIcon:nil downIndexImage:@"meat_shop_riskwarning" textColor:mMainTextColor font:mFont(13)];
                cell.detailTextLabel.attributedText = aims;
            }else{
                [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, KScreenWidth-30)];
                cell.detailTextLabel.text = self.detailTitles[indexPath.row -1];
            }
        }
        
        
        cell.textLabel.text = self.titles[indexPath.row -1];
        cell.textLabel.textColor = UIColorFromRGB(0x666666);
        cell.textLabel.font = mFont(13);
        
        cell.detailTextLabel.textColor = mMainTextColor;
        cell.detailTextLabel.font = mFont(13);
        
        if (indexPath.row == self.titles.count) {
            cell.textLabel.textColor = mMainTextColor;
            cell.detailTextLabel.textColor = UIColorFromRGB(0xF25C62);
        }
        
        return cell;
    }
    
    
    return [UITableViewCell new];

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MeatOrderBox *box = [[MeatOrderBox alloc] init];
    [box showView];
    
}

#pragma mark - setter and getter

- (MeatTipLabel *)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[MeatTipLabel alloc] init];
        _tipLabel.height = 34;
        
        NSString *contentStr = @"该猪已欠费相关提示语该猪已欠费相关提示语该猪已欠！";
        _tipLabel.text = contentStr;
        [_tipLabel sizeToFit];
    }
    return _tipLabel;
}


- (MeatPayView *)footer{
    if (!_footer) {
        _footer = [[MeatPayView alloc] init];
        _footer.type = MeatPayType_NoTip;
        
        //全选Block
        @weakify(self);
        //支付Block
        _footer.payCartBlock = ^{
            @strongify(self);
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"配送范围限制" message:@"选择该地址可能需要重新挑选商品" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [cancelAction setValue:UIColorFromRGB(0x363636) forKey:@"titleTextColor"];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                MeatPayViewController *vc = [[MeatPayViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }];
            [okAction setValue:mMainColor forKey:@"titleTextColor"];
            
            
            [alert addAction:cancelAction];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        };
        
    }
    return _footer;
}



- (NSArray *)titles{
    
    if (!_titles) {
        _titles = @[@"三元仔猪", @"仔猪编号", @"当前日龄", @"被监督人次", @"已支付费用", @"售卖价格"];
    }
    return _titles;
}


- (NSArray *)detailTitles{
    
    if (!_detailTitles) {
        _detailTitles = @[@"A201812240001", @"80", @"2019-08-24", @"64937", @"¥1834.98", @"¥1834.98"];
    }
    return _detailTitles;
}


#pragma mark - override methods

- (void)registerTableViewCell{
    
    [self.tableView registerClass:[MeatOrderListCell class] forCellReuseIdentifier:@"MeatOrderListCell"];
    [self.tableView registerClass:[MeatOrderLocationCell class] forCellReuseIdentifier:@"MeatOrderLocationCell"];
    
    self.tableView.height = KScreenHeight - (TJ_NavBarHeight);
    self.tableView.separatorColor = mIconLineColor;

    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 10)];
    view.backgroundColor = mBackgroudColor;
    self.tableView.tableFooterView = view;
}


@end
